package com.vieewer.btdemo.view.microsoft.qna;// httpclient-4.5.3.jar
// httpcore-4.4.6.jar
// commons-logging-1.2.jar

import java.net.URI;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.vieewer.btdemo.R;
import com.vieewer.btdemo.base.BaseActivity;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.StringEntity;
import org.jetbrains.annotations.Nullable;

// 1. Replace variable values with your own from QnA Maker Publish page
// 2. Compile with: javac -cp "lib/*" GetAnswer.java
// 3. Execute with: java -cp ".;lib/*" GetAnswer

public class GetAnswer extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        TextView test = findViewById(R.id.txt_test);
        test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickTest();
            }
        });

    }

    private void onClickTest() {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    // Represents the various elements used to create HTTP request URIs
                    // for QnA Maker operations.
                    // From Publish Page: HOST
                    // Example: https://YOUR-RESOURCE-NAME.azurewebsites.net/qnamaker
                    String host = "https://qnavieewer.azurewebsites.net/qnamaker";

                    // Management APIs postpend the version to the route
                    // From Publish Page, value after POST
                    // Example: /knowledgebases/ZZZ15f8c-d01b-4698-a2de-85b0dbf3358c/generateAnswer
                    String service = "/knowledgebases/b8063fdc-3439-4fbb-8c7f-ac1872e7ecec/generateAnswer";

                    // Authorization endpoint key
                    // From Publish Page
                    String endpointKey = "ffb577ce-5cd8-49ab-a8d9-404f4e559649";

                    // JSON format for passing question to service
                    String question = "{ 'question' : 'Is the QnA Maker Service free?', 'top' : 3 }";

                    // Create http client
                    HttpClient httpclient = HttpClients.createDefault();

                    // Add host + service to get full URI
                    String answer_uri = host + service;
                    System.out.println(answer_uri);

                    //
                    HttpPost request = new HttpPost(answer_uri);

                    // set question
                    StringEntity entity = new StringEntity(question);
                    request.setEntity(entity);
                    request.setHeader("Content-type", "application/json");

                    // set authorization
                    request.setHeader("Authorization",endpointKey);
                    System.out.println("authorizationHeader " + endpointKey);

                    // Send request to Azure service, get response
                    HttpResponse response = httpclient.execute(request);

                    HttpEntity entityResponse = response.getEntity();

                    if (entityResponse != null)
                    {
                        Log.i("yjj", "response back!");
                        Log.i("yjj", EntityUtils.toString(entityResponse));
                    }
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }


            }
        }).start();


    }


}